# Esta clase controlla el login/logout del sistema.  
class SessionsController < ApplicationController

	before_filter :login_home, :only => [:new]
	
  def new
  end

# Crea la nueva sesión mediante el ingreso de un usuario existente al sistema (login).
  def create
    self.current_user = User.authenticate(params[:login], params[:password])
    if logged_in?
      if params[:remember_me] == "1"
        current_user.remember_me unless current_user.remember_token?
        cookies[:auth_token] = { :value => self.current_user.remember_token , :expires => self.current_user.remember_token_expires_at }
      end
      redirect_back_or_default(welcome_user_url( :user_id => self.current_user.id ))
      flash[:notice] = "Logged in successfully"
    else
      flash_warning :message => "El usuario y/o password no son correctos."  
      render :action => 'new'
    end
  end


# Elimina la sesión debido a una salida del sistema (logout).
  def destroy
    self.current_user.forget_me if logged_in?
    cookies.delete :auth_token
    reset_session
    flash[:notice] = "You have been logged out."
    redirect_back_or_default('/cuentas')
  end
end
